package com.bnzjy.srb.core.controller.admin;

import com.bnzjy.common.exception.Assert;
import com.bnzjy.common.result.R;
import com.bnzjy.common.result.ResponseEnum;
import com.bnzjy.srb.core.pojo.entity.IntegralGrade;
import com.bnzjy.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

//@CrossOrigin  // 配置跨域
@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "积分等级管理")
@Slf4j
public class AdminIntegralGradeController {
    
    @Resource
    private IntegralGradeService integralGradeService;
    
    /**
     * 查询 IntegralGrade 所有信息
     *
     * @return
     */
    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public R listAll() {
    
        log.info("hi i'm helen");
        log.warn("warning!!!");
        log.error("it's a error");
        
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list", list).message("获取列表成功");
    }
    
    /**
     * 根据id 进行逻辑删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除积分等级", notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(
        @ApiParam(value = "数据id", required = true)
        @PathVariable Long id) {
        boolean result = integralGradeService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }
    
    /**
     * 新增数据
     * @param integralGrade
     * @return
     */
    @ApiOperation(value = "保存积分等级")
    @PostMapping("/save")
    public R save(@ApiParam(value = "积分等级对象", required = true)
                  @RequestBody IntegralGrade integralGrade) {
    
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        
        boolean result = integralGradeService.save(integralGrade);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }
    
    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation("根据id获取积分等级")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "数据id", required = true)
                     @PathVariable Long id) {
        IntegralGrade byId = integralGradeService.getById(id);
        if (byId != null) {
            return R.ok().data("记录", byId);
        } else {
            return R.error().message("数据不存在");
        }
    }
    
    @ApiOperation("更新积分等级")
    @PutMapping("/update")
    public R updateById(@ApiParam(value = "积分等级对象", required = true)
                        @RequestBody IntegralGrade integralGrade) {
        
//        if (integralGrade.getBorrowAmount() == null) {
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
//        }
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
       
        boolean results = integralGradeService.updateById(integralGrade);
        if (results) {
            return R.ok().message("更新成功");
        } else {
            return R.error().message("更新失败");
        }
    }
}
